<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//文章
class Article extends MY_Controller {

	function __construct(){
		parent::__construct();
		
		$this->load->model('Index/ArticleModel', 'item');
	}	
	
	
	//start
	/**
	* @name 获取列表
	* @param page false int 页面
	* @param size false int 页面数据数量
	* @param type false int 文章类型
	* @return 
	*/
	//end
	public function get_list(){		
		$user_id = $this->user_id;
		$page	= $this->input->post('page',null,1);
		$size	= $this->input->post('size',null,10);
		$type	= $this->input->post('type',null,0);//文章类型
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" => $user_id,
			"type"	  => $type
		);
		
		$res = $this->item->get_list($arr,$page,$size);
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
	
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//start
	/**
	* @name 获取详细
	* @param id false int ID
	* @return 
	*/
	//end
	public function get_info(){		
		$user_id = $this->user_id;
		$id	  = $this->input->post('id',null,'0');
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$res = $this->item->get_info($id,$user_id);
		
		$data['result_code'] = 0;
		$data['message']     = '成功';
		$data['data'] 		 = $res;
	
		$this->out($data);
	}
	
	//start
	/**
	* @name 获取全部
	* @param id false int ID
	* @return 
	*/
	//end
	public function get_lists(){
		$res = $this->item->get_lists();
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//文章类型
	public function get_type(){
		
		$this->load->model('Index/ArticleTypeModel', 'atype');
		$res = $this->atype->get_lists();
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//添加点赞
	public function add_like(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('文章不存在');
		}
		
		$this->load->model('Index/ArticleActiveModel', 'active');
		$row = $this->active->user_get_info($user_id,$id);
		if(!$row){
			$arr = array(
				"user_id" 	=> $user_id,
				"aid" 		=> $id,
				"likes" 	=> 1,
			);
			$res = $this->active->add($arr);		
		}else{
			if($row['likes']==0){			
				$res = $this->active->edit(array("likes"=>1)," user_id='{$user_id}' and aid='{$id}' ");				
			}else{
				$this->out(array("result_code"=>1,"message"=>"您已经点赞过了","data"=>$info['likes']));
			}
		}
		
		//添加总数
		$sum = $info['likes']+1;
		$resadd = $this->item->edit(array("likes"=>$sum)," id='{$id}' ");
		
		$this->out(array("message"=>'点赞成功',"data"=>$sum));
	}
	
	//取消点赞
	public function del_like(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('文章不存在');
		}
		
		$this->load->model('Index/ArticleActiveModel', 'active');
		$row = $this->active->user_get_info($user_id,$id);
		if($row){			
			$res = $this->active->edit(array("likes"=>0)," user_id='{$user_id}' and aid='{$id}' ");		
		}else{
			$this->out(array("message"=>'取消成功'));
		}
		
		//减总数
		$sum = $info['likes']-1;
		$sum = $sum<0?0:$sum;
		
		$resadd = $this->item->edit(array("likes"=>$sum)," id='{$id}' ");
		
		$this->out(array("message"=>'取消成功'));
	}
	
	//添加收藏
	public function add_collection(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('文章不存在');
		}
		
		$this->load->model('Index/ArticleActiveModel', 'active');
		$row = $this->active->user_get_info($user_id,$id);
		if(!$row){
			$arr = array(
				"user_id" 	=> $user_id,
				"aid" 		=> $id,
				"collection"=> 1,
			);
			$res = $this->active->add($arr);		
		}else{
			if($row['collection']==0){			
				$res = $this->active->edit(array("collection"=>1)," user_id='{$user_id}' and aid='{$id}' ");				
			}else{
				$this->out(array("result_code"=>1,"message"=>"您已经收藏过了","data"=>$info['collection']));
			}
		}
		
		//添加总数
		$sum = $info['collection']+1;
		$resadd = $this->item->edit(array("collection"=>$sum)," id='{$id}' ");
		
		$this->out(array("message"=>'收藏成功',"data"=>$sum));
	}
	
	//取消收藏
	public function del_collection(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('文章不存在');
		}
		
		$this->load->model('Index/ArticleActiveModel', 'active');
		$row = $this->active->user_get_info($user_id,$id);
		if($row){			
			$res = $this->active->edit(array("collection"=>0)," user_id='{$user_id}' and aid='{$id}' ");		
		}else{
			$this->out(array("message"=>'取消成功'));
		}
		
		//减总数
		$sum = $info['collection']-1;
		$sum = $sum<0?0:$sum;
		
		$resadd = $this->item->edit(array("collection"=>$sum)," id='{$id}' ");
		
		$this->out(array("message"=>'取消成功'));
	}
	
	//分享
	public function share(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');
		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('文章不存在');
		}
		
		$this->load->model('Index/ArticleActiveModel', 'active');
		$row = $this->active->user_get_info($user_id,$id);
		if(!$row){
			$arr = array(
				"user_id" 	=> $user_id,
				"aid" 		=> $id,
				"share"=> 1,
			);
			$res = $this->active->add($arr);		
		}else{
			if($row['share']==0){			
				$res = $this->active->edit(array("share"=>1)," user_id='{$user_id}' and aid='{$id}' ");				
			}
		}
		
		//添加总数
		$sum = $info['share']+1;
		$resadd = $this->item->edit(array("share"=>$sum)," id='{$id}' ");
		
		$this->out(array("message"=>'收藏成功',"data"=>$sum));		
	}
	
}
